<template>
  <div class="course-info">
    <van-cell-group :style="styleObj">
      <van-cell class="couse-img">
        <van-image :src="info.courseImgUrl"></van-image>
      </van-cell>
      <van-cell class="course-des" :border="false">
        <h2 v-text="info.courseName"></h2>
        <p v-text="info.previewFirstField"></p>
        <div class="course-sale-info">
          <p class="course-price">
            <span class="discounts">￥{{ info.discounts }}</span>
            <span>￥{{ info.price }}</span>
          </p>
          <span class="tag">{{ info.sales }}人已购</span>
          <span class="tag">每周三、周五</span>
        </div>
      </van-cell>
      <van-cell class="corse-detail">
        <van-tabs sticky scrollspy>
          <van-tab title="详情">
            <div v-html="info.courseDescription"></div>
          </van-tab>
          <van-tab title="内容">
            <course-section :section-list="section"></course-section>
          </van-tab>
        </van-tabs>
      </van-cell>
    </van-cell-group>
    <van-tabbar v-if="!info.isBuy">
      <div class="course-price">
        <span v-text="info.discountsTag"></span>
        <span class="discounts">￥{{ info.discounts }}</span>
        <span>￥{{ info.price }}</span>
      </div>
      <van-button type="primary" @click="handlePay">立即购买</van-button>
    </van-tabbar>
  </div>
</template>

<script>
import { getCourseById, getCourseLesson } from '@/services/course'
import courseSection from './conponents/section'

export default {
  name: 'courseInfo',
  props: {
    courseId: {
      type: [Number, String],
      require: true
    }
  },
  data () {
    return {
      info: {},
      section: {},
      styleObj: {}
    }
  },
  created () {
    this.getInfo()
    this.getLesson()
  },
  methods: {
    handlePay () {
      if (this.$store.state.user) {
        this.$router.push({ name: 'pay', params: { courseId: this.courseId } })
      } else {
        this.$router.push({ name: 'login', query: { redirect: this.$route.fullPath } })
      }
    },
    async getInfo () {
      const { data } = await getCourseById({ courseId: this.courseId })
      if (data.state === 1) {
        this.info = data.content
      }
      console.log(data)
      if (data.content.isBuy) {
        this.styleObj.bottom = 0
      }
    },
    async getLesson () {
      const { data } = await getCourseLesson({ courseId: this.courseId })
      if (data.state === 1) {
        this.section = data.content.courseSectionList
      }
    }
  },
  components: {
    courseSection
  }
}
</script>

<style lang="scss" scoped>
.van-cell {
  padding: 0;
}
.course-img {
  height: 280px;
}
.course-des {
  padding: 10px 20px;
  height: 150px;
}

.course-sale-info {
  display: flex;
}
.course-sale-info .course-price {
  flex: 1;
  margin: 0;
}

.discounts {
  color: coral;
  font-size: 24px;
}

.course-sale-info .tag {
  background-color: #f8ffff;
  font-weight: 700;
  margin-left: 10px;
  padding: 7px;
  color: #666;
  line-height: 15px;
  border-radius: 3px;
}
.van-cell-group {
  width: 100%;
  position: fixed;
  top: 0;
  bottom: 50px;
  overflow-y: auto;
}
.van-tabbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  line-height: 50px;
  padding: 0 20px;
  box-sizing: border-box;
}
span {
  font-size: 14px;
}

.van-button {
  width: 50%;
  height: 80%;
}
</style>
